layui.use(['table', 'admin', 'ax', 'ztree'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    /**
     * 系统管理--采购管理
     */
    var Procurement = {
        tableId: "procurementTable",

    };

    /**
     * 初始化表格的列
     */
    Procurement.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'Id', hide: true, sort: true, title: 'id'},
            {field: 'Name', sort: true, title: '商品名称'},
            {field: 'uprice', sort: true, title: '单价'},
            {field: 'Weight', sort: true, title: '重量'},
            {field: 'Phase', sort: true, title: '品相'},
            {field: 'Grade', sort: true, title: '量级'},
            {field: 'Number', sort: true, title: '件数'},
            {field: 'tprice', sort: true, title: '总价格'},
            {field: 'Personnel', sort: true, title: '采购人员'},
            {field: 'Time', sort: true, title: '时间'},
            {field: 'Cost', sort: true, title: '费用'},
            {field: 'Address', sort: true, title: '详细地址'},
            {align: 'Center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Procurement.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['address'] =  $("#address").val();
        table.reload(Procurement.tableId, {where: queryData});
    };

    /**
     * 弹出添加
     */
    Procurement.openAddProcurement = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加部门',
            content: Feng.ctxPath + '/fruit_procurement/procurement_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Procurement.tableId);
            }
        });
    };




    /**
     * 导出excel按钮
     */7
    Procurement.exportExcel = function () {
        var checkRows = table.checkStatus(Procurement.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑部门
     *
     * @param data 点击按钮时候的行数据
     */
    Procurement.onEditProcurement = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改',
            content: Feng.ctxPath + '/fruit_procurement/update?fruitProcurementId=' + data.Id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Procurement.tableId);
            }
        });
    };


    /**
     * 点击查看客户详情
     *
     * @param data 点击按钮时候的行数据
     */
    Procurement.onDetailProcurement = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '客户详情',
            content: Feng.ctxPath + '/fruit_procurement/detail?fruitProcurementId=' + data.Id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Procurement.tableId);
            }
        });
    };
     /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Procurement.onDeleteProcurement = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/fruit_procurement/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Procurement.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("fruitProcurementId", data.Id);
            ajax.start();
        };
        Feng.confirm("是否删除 " + data.name + "?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Procurement.tableId,
        url: Feng.ctxPath + '/fruit_procurement/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Procurement.initColumn()
    });


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Procurement.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Procurement.openAddProcurement();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Procurement.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Procurement.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Procurement.onEditProcurement(data);
        } else if (layEvent === 'delete') {
            Procurement.onDeleteProcurement(data);
        }else if (layEvent === 'detail') {
            Procurement.onDetailCustomer(data);
        }
    });
});
